﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
namespace DevEx.Thanh.Class
{
    class CNgayDauKyController
    {
        QLVTDataContext objDataContext;
        public IQueryable<tblNGAYDAUKY> SelectAllRecord()
	    {
		    try {
			    return (from u in objDataContext.tblNGAYDAUKies select u);
		    } catch (Exception ex)
		    {
		        return null;
		    }
	    }
        public tblNGAYDAUKY SelectRecordByID(DateTime _ngay)
	    {
		    try {
			    return (from u in objDataContext.tblNGAYDAUKies where u.kychuyen == _ngay select u).Single();
		    } catch (Exception ex)
		    {
		        return null;
		    }
	    }
        public Boolean CheckExist(int year)
        {
            DateTime ngaydauky = new DateTime(year, 1, 1);
            return (objDataContext.tblNGAYDAUKies.Any(ngay => ngay.kychuyen == ngaydauky));
        }
        public DateTime InsertRecord(tblNGAYDAUKY LocalTable)
        {
            try
            {
                objDataContext.tblNGAYDAUKies.InsertOnSubmit(LocalTable);
                objDataContext.SubmitChanges();
                return LocalTable.kychuyen;
            }
            catch (Exception ex)
            {
                return DateTime.MinValue;
            }
        }
        public void UpdateRecord(tblNGAYDAUKY LocalTable)
        {
            try
            {
                objDataContext.tblNGAYDAUKies.Attach(LocalTable);
                objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable);
                objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void UpdateRecordWithoutAttach(tblNGAYDAUKY LocalTable)
        {
            try
            {
                objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable);
                objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DateTime ChuyenDuLieuDauNam(int year)
        {
            objDataContext.sp_T_ChuyenDuLieuDauNam();
            //thêm thông tin chuyển dữ liệu vào
            tblNGAYDAUKY tbl = new tblNGAYDAUKY();
            tbl.kychuyen = new DateTime(year, 1, 1);
            tbl.ngaygiochuyen = DateTime.Now;
            tbl.nguoichuyen = GlobalVar.CurrentUser.FullName;
            return InsertRecord(tbl);
        }
        public CNgayDauKyController()
        {
            objDataContext = new QLVTDataContext( );
        }
        public CNgayDauKyController(QLVTDataContext  _obj)
        {
            objDataContext = _obj;
        }
    }
}
